package com.hebeu.group.service.impl;

import com.hebeu.group.mapper.OrderDetailsMapper;
import com.hebeu.group.mapper.OrdersMapper;
import com.hebeu.group.pojo.OrderDetails;
import com.hebeu.group.pojo.OrderDetailsExample;
import com.hebeu.group.pojo.Orders;
import com.hebeu.group.pojo.OrdersExample;
import com.hebeu.group.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    private OrdersMapper ordersMapper;
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    public OrdersServiceImpl(OrdersMapper ordersMapper, OrderDetailsMapper orderDetailsMapper) {
        this.ordersMapper = ordersMapper;
        this.orderDetailsMapper = orderDetailsMapper;
    }

    @Override
    public List<Orders> getAllOrders() {
        OrdersExample ordersExample = new OrdersExample();
        OrdersExample.Criteria criteria = ordersExample.createCriteria();
        criteria.andOIdIsNotNull();
        List<Orders> orders = ordersMapper.selectByExample(ordersExample);
        return orders;
    }

    @Override
    public Orders selectOrderById(Integer orderId) {
        OrdersExample ordersExample = new OrdersExample();
        OrdersExample.Criteria criteria = ordersExample.createCriteria();
        criteria.andOIdEqualTo(orderId);
        List<Orders> orders = ordersMapper.selectByExample(ordersExample);
        return orders.get(0);
    }

    @Override
    public List<OrderDetails> selectOrderDetailsById(Integer orderId) {
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        OrderDetailsExample.Criteria criteria = orderDetailsExample.createCriteria();
        criteria.andOIdEqualTo(orderId);
        return orderDetailsMapper.selectByExample(orderDetailsExample);
    }


    @Override
    public void addOrder(Orders orders) {
        ordersMapper.insert(orders);
    }
}
